package com.jsyl.lwbk.business.service.impl;

import com.jsyl.lwbk.business.domain.ChildCenter;
import com.jsyl.lwbk.business.dto.ChildDTO;
import com.jsyl.lwbk.business.constant.MedicalDiagnosisEnum;
import com.jsyl.lwbk.business.domain.Child;
import com.jsyl.lwbk.business.mapper.ChildCenterMapper;
import com.jsyl.lwbk.business.mapper.ChildMapper;
import com.jsyl.lwbk.business.service.IChildService;
import com.jsyl.lwbk.business.vo.ChildVO;
import com.jsyl.lwbk.common.core.text.Convert;
import com.jsyl.lwbk.common.exception.BusinessException;
import com.jsyl.lwbk.common.utils.DateUtils;
import com.jsyl.lwbk.common.utils.StringUtils;
import com.jsyl.lwbk.system.mapper.SysUserMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ChildServiceImpl implements IChildService {

    @Autowired
    ChildMapper childMapper;
    @Autowired
    private ChildCenterMapper childCenterMapper;

    @Autowired
    SysUserMapper sysUserMapper;

    @Override
    public List<ChildVO> findChildList(ChildDTO childDTO, List<String> roleNameList, Long userId) {
        List<ChildVO> list = new ArrayList<>();
        if (childDTO.getCenterId() == null || childDTO.getCenterId() == "") {
            throw new BusinessException("请选择中心。");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String year = sdf.format(new Date()).substring(0, 4);
        String beginAge = childDTO.getBeginAge();
        if (!StringUtils.isNotEmpty(beginAge)) {
            beginAge = "0";
        }
        childDTO.setBeginAge(String.valueOf((Integer.valueOf(year) - Integer.valueOf(beginAge))));
        String endAge = childDTO.getEndAge();
        if (!StringUtils.isNotEmpty(endAge)) {
            endAge = "15";
        }
        childDTO.setEndAge(String.valueOf((Integer.valueOf(year) - Integer.valueOf(endAge))));
        if (CollectionUtils.isEmpty(roleNameList)) {
            throw new BusinessException("用户对应的角色不存在，请联系管理员。");
        } else {
            list = childMapper.findChildList(childDTO);
//            for (String roleName : roleNameList) {
//                if (roleName.equals("admin") || roleName.equals("teacher")) {

//                } else {
//                    list = childMapper.findChildList(childDTO, userId);
//                }
//            }
        }
        for (ChildVO childVO : list) {
            if (StringUtils.isNotEmpty(childVO.getBirthYear())) {
                Integer ageYear = Convert.toInt(childVO.getBirthYear(), DateUtils.getYear(new Date()));
                Integer ageMonth = Convert.toInt(childVO.getBirthMonth(), 0);

                childVO.setBirthYear(String.valueOf(ageYear));
                childVO.setBirthMonth(String.valueOf(ageMonth));
                childVO.setAge((String) DateUtils.toAge(ageYear, ageMonth).get("age"));
            }
            childVO.setMedicalDiagnosisStr(MedicalDiagnosisEnum.getMedicalDiagnosisStr(childVO.getMedicalDiagnosis()));

        }
        return list;
    }

    @Override
    public Child findById(Long id) {
        return childMapper.selectById(id);
    }

    @Override
    public int insertChild(Child child) {
        child.setStatus(0);
        child.setCreateTime(DateUtils.getNowDate());
        int result =this.childMapper.insert(child);
        ChildCenter childCenter =new ChildCenter();
        childCenter.setChildId(child.getId());
        childCenter.setCenterId(child.getCenterId());
        childCenter.setIsMember(child.getIsMember());
        childCenter.setStatus(0);
        childCenter.setCreateTime(DateUtils.getNowDate());
        this.childCenterMapper.insert(childCenter);
        return result;
    }

    @Override
    public int updateChild(Child child) {
        child.setUpdateTime(DateUtils.getNowDate());
        return this.childMapper.update(child);
    }
}
